$(function () {
    queryBlogLink();
    initiRight();
});

layui.use('layer', function () {
    var layer = layui.layer;  //layer初始化
    //提交申请
    $('#link-submit').on('click',function () {
        var encryptKey = uuid(16,16);
        var linkUrl = $('#linkUrl').val();
        var linkName = $('#linkName').val();
        var linkImage = $('#linkImage').val();
        var linkDesc = $('#linkDesc').val();
        var linkEmail = $('#linkEmail').val();
       /* if(!checkIsNullOrEmpty(linkUrl)) {
            $('#linkUrl-tips').html('请输入网站地址');
            $('#linkUrl-tips').show();
            return false;
        }*/
        var formData = {
            'linkUrl': encrypt(linkUrl,encryptKey),
            'linkName': encrypt(linkName,encryptKey),
            'linkImage': encrypt(linkImage,encryptKey),
            'linkDesc': encrypt(linkDesc,encryptKey),
            'linkEmail': encrypt(linkEmail,encryptKey),
            'encryptKey': encryptKey
        }
        $.ajax({
            beforeSend: function() {
                layer.load(2);
            },
            type: 'POST',
            url: '/blog/insertBlogLink',
            data: JSON.stringify(formData),
            dataType: 'json',
            contentType: 'application/json; charset=UTF-8',// 解决415错误
            success: function (res) {
                if(res.code == 200) {
                    $("#link-form")[0].reset();
                    layer.msg(res.msg, { icon: 1 });
                    layer.closeAll("loading");
                }else {
                    layer.closeAll("loading");
                    layer.msg(res.msg, {icon: 5, time: 5000});
                }
            },
            error: function() {
                layer.closeAll("loading");
            }
        });
    });

});

/**
 * 加载右侧侧边栏信息
 */
function initiRight() {
    //调用后台
    $.ajax({
        type: 'POST',
        url: '/blog/queryRightInfo',
        dataType: 'json',
        contentType: 'application/json; charset=UTF-8',// 解决415错误
        success: function (res) {
            if('200' == res.code) {
                initRank(res.data.rankBeanList); //排行榜信息
                initTag(res.data.tagList); //标签墙信息
            }
        },
        error: function() {
            layer.alert()
        }
    });

}

/**
 * 友链列表查询
 */
function queryBlogLink() {
    $.ajax({
        type: 'POST',
        async : false,
        url: '/blog/queryBlogLink',
        dataType: 'json',
        contentType: 'application/json; charset=UTF-8',// 解决415错误
        success: function (res) {
            var html = '';
            if('200' == res.code) {
                $.each(res.data,function (k,v) {
                    var desc = v.linkDesc;
                    html += '<li>';
                    html += '<a target="_blank" href="'+v.linkUrl+'" title="'+v.linkName+'" class="friendlink-item">';
                    html += '<p class="friendlink-item-pic"><img src="'+v.linkImage+'" alt="'+v.linkName+'" /></p>';
                    html += '<p class="friendlink-item-title">'+v.linkName+'</p>';
                    if(desc.length > 10) {
                        html += '<p class="friendlink-item-domain">'+desc.substr(0,10)+'...</p>';
                        html += '<p class="none">'+desc+'</p>';
                    }else {
                        html += '<p class="friendlink-item-domain">'+desc+'</p>';
                    }
                    html += '</a>';
                    html += '</li>';
                })
                $('.friendlink').html(html);
            }
        }
    });
}


//初始化标签墙信息
function initTag(tagData) {
    var html = '';
    html += '<div class="cloud">';
    html += '<h2 class="cloud-title">标签墙</h2>';
    html += '<ul>';
    $.each(tagData,function (k,v) {
        html += '<a  href="index.html?tagId='+v.tagId+'" title="'+v.tagName+'">'+v.tagName+'('+v.tagCnt+')</a>';
    })
    html += '</ul>';
    html += '</div>';
    //加载标签墙信息
    $('.blog-main-right').append(html);
}

//初始化热文信息
function initRank(rankData) {
    var html = '';
    html += '<div class="blog-module shadow">';
    html += '<div class="blog-module-title">最热阅读</div>';
    html += '<ul class="fa-ul blog-module-ul">';
    $.each(rankData,function (k,v) {
        html += '<li><i class="fa-li fa fa-hand-o-right"></i><a href="detail.html?id='+v.id+'">'+v.title+'</a></li>';
    });
    html += '</ul>';
    html += '</div>';
    //加载热文信息
    $('.blog-main-right').append(html);
}